package interfaceDEMO;

import interfaceDEMO.Utils.InterfacePropertoes;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import top.jfunc.json.impl.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wq
 * @since 2022-06-21 19:15
 */

public class Login {
    public static void login(JSONObject jsonObject) throws IOException {
        HttpPost post = new HttpPost(InterfacePropertoes.loginurl);
        post.setEntity(new StringEntity(jsonObject.toString()));
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(post);
        String string = EntityUtils.toString(response.getEntity());
        Pattern compile = Pattern.compile("\"token\":\"(.*)\"");
        Matcher matcher = compile.matcher(string);
        if (matcher.find()){
            String group = matcher.group(1);
            InterfacePropertoes.token=group;
        }
    }
}
